<?php
/**
 * 会员管理
 * @author hushuai
 *
 */
class CustomAction extends CommonAction{
	
	
	/**
	 * 会员列表
	 */
	public function index(){
		
		if(IS_AJAX){
			
			$desc    = $this->_param("orderDirection","trim","desc");;
			$order   = $this->_param("orderName","trim","id");
			$pageNum = $this->_param("pageNum","intval",C('PAGE_NUMS'));
			$nowPage = $this->_param("nowPage","intval",1);
			$where   = $this->_param('where','trim','');
			$map   = array("where"=>$where,"pageNum"=>$pageNum, "nowPage"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order); 
			$json  = sendRest("MMS/Custom/index",$map);
			//echo createRestUrl("MMS/Custom/index", $map);exit();
			
			$data  = $json['data'];
			$count = $data['count'];
			
			$totalPage = ceil($count/$pageNum);
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
			
			foreach($data['list'] AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'       => $row['id'],
						'realname' => $row['realname'],
						'mobile'   => $row['mobile'],
						'phone'    => $row['phone'],
						'email'    => $row['email'],
						'isreg'    => $row['isreg']==1?"已注册":"未注册",
						'sex'      => $row['sex']==1?"先生":"女士",
						'addtime'  => date('Y-m-d',$row['addtime']),
						'status'   => $row['status']==1?"正常":"冻结",
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,"JSON");
		}else{
			$map['isdel'] = 1;
			$json = sendRest("MMS/Level/index",$map);
			$this->assign("levelList",$json['data']);
			
			$jsons = sendRest("MMS/Group/index");
			$this->assign("details",$jsons['data']);
			
			$this->display();
		}
	}
	
	/**
	 * 会员列表
	 */
	public function search(){
	
		if(IS_AJAX){
			
			$desc    = $this->_get("orderDirection","trim",   "desc");;
			$order   = $this->_get("orderName",     "trim",   "addtime");
			$pageNum = $this->_get("pageNum",       "intval",  C('PAGE_NUMS'));
			$nowPage = $this->_get("p",             "intval",  1);
			
			$vipno     = $this->_get("vipno",    "intval", 0);
			$mobile    = $this->_get("mobile",   "trim",   "");
			$realname  = $this->_get("realname", "trim",   "");
			$groupid   = $this->_get("groupid",  "intval", 0);
			
			$map   = array("vipno"=>$vipno,"mobile"=>$mobile,"realname"=>$realname,"groupid"=>$groupid, "pageNum"=>$pageNum, "p"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);
			
			$json  = sendRest("MMS/Custom/search",$map);
			$data  = $json['data'];
			$count = $data['count'];
				
			$totalPage = ceil($count/$pageNum);
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
				
			foreach($data['list'] AS $row){
				$entry = array('id'=>$row['id'],
						'cell'=>array(
							'id'       => $row['id'],
							'username' => $row['username'],
							'mobile'   => $row['mobile'],
							'phone'    => $row['phone'],
							'email'    => $row['email'],
							'sex'      => $row['sex']==1?"先生":"女士",
							'addtime'  => date('Y-m-d',$row['addtime']),
							'status'   => $row['status']==1?"正常":"冻结",
						),
				);
				$jsonData['rows'][] = $entry;
			}
				
			$this->ajaxReturn($jsonData,"JSON");
		}
	}
	
	/**
	 * 添加会员
	 */
	public function add(){
		
		if(IS_AJAX){
			$json = sendRest("MMS/Custom/add",$_POST,'post');
			$this->ajaxReturn($json);
		}else{
			$this->display();
		}
	}

	/**
	 * 会员添加地址
	 */
	public function address(){
		if(IS_AJAX){
			
			$userid  = $this->_param('userid','intval',0);
			$addid   = $this->_param('addid','intval',0);
			$detail  = $this->_param('detail','trim','');
			$member  = M('Member')->find($userid);
			if(!$member['id']){
				$this->ajaxReturn(array('status'=>0,'info'=>'请选择一个会员'));
			}
			if(!$_POST['address']){
				$this->ajaxReturn(array('status'=>0,'info'=>'请通过地址关键词或者地图标注确认会员的具体位置'));
			}
			
			if($addid){
				$address = M('Site')->find($addid);
				if(!$address['id']){
					$this->ajaxReturn(array('status'=>0,'info'=>'请选择一个常用地址'));
				}
				$map['longitude']= $address['longitude'];
				$map['latitude'] = $address['latitude'];
				$map['address']  = $address['address'].$detail;
				$map['ads']      = $address['address'];
			}else{
				$map['longitude']= $_POST['pointlng'];
				$map['latitude'] = $_POST['pointlat'];
				$map['address']  = $_POST['address'].$detail;
				$map['ads']      = $_POST['address'];
			}
			
			$map['userid']   = $userid;
			$map['tableid']  = 'Member';
			$map['realname'] = $member['realname'];
			$map['mobile']   = $member['mobile'];
			$map['city']     = $this->_param('city','intval',0);
			$map['detail']   = $detail;
			$map['addtime']  = time();
			$map['addressid']= $addid;
			
			$data = M('Address')->add($map);
			if($data){
				$this->ajaxReturn(array('status'=>1,'data'=>$data));
			}else{
				$this->ajaxReturn(array('status'=>0,'info'=>'地址添加失败，请稍后重试'));
			}
		}else{
			$this->display();
		}
	}

	//添加会员地址，标注信息点
	public function outletLbs(){
		if(IS_AJAX){
			$pointlng = $this->_param("pointlng","trim","");
			$pointlat = $this->_param("pointlat","trim","");
				
			//根据百度地图pai获取用户具体位置
			$url = "http://api.map.baidu.com/geocoder/v2/?ak=v91aIepsmM86gClKu180Zsdb&location=".$pointlat.",".$pointlng."&output=json";
			$data = json_decode(file_get_contents($url),true);

			$address = "";
			if($data['status']==0){
				$address = $data['result']['formatted_address'];
			}
			$this->ajaxReturn($address);
		}else{
			$address = $this->_param('address','trim','北京');
			
			$this->assign('address',$address);
			$this->display();
		}
	}

	//删除会员地址
	public function deladdress(){
		$adsid  = $this->_param('id','intval',0);
		$userid = $this->_param('user','intval',0);

		if($adsid && $userid){

			$model  = M('Address');
			$detail = $model->find($adsid);
			if($detail['id'] && $detail['userid']==$userid){
				$where['id']     = $adsid;
				$where['userid'] = $userid;
				$data = $model->where($where)->delete();
				if($data){
					$this->ajaxReturn(array('status'=>1));
				}else{
					$this->ajaxReturn(array('status'=>0,'info'=>'删除失败，请稍后重试'));
				}
			}else{
				$this->ajaxReturn(array('status'=>0,'info'=>'非法操作'));
			}
		}else{
			$this->ajaxReturn(array('status'=>0,'info'=>'非法操作'));
		}
	}
	
	/**
	 * 批量导入会员
	 */
	public function import(){
		
		if(IS_POST){
			$fileurl = $this->_param("fileurl");
			$cityid  = $this->_param('cityid');
			//dump($fileurl);exit;
			$data    = D('Custom')->import($fileurl,$cityid);
			$this->ajaxReturn($data);
		}else{

			$list = C('CITYID');
			$this->assign('list',$list);

			$this->assign("UPLOAD_PATH",UPLOAD_PATH);
			$this->display();
		}
	}
	
	/**
	 * 会员资料修改
	 */
	public function edit(){
		
		if(IS_AJAX){
			$json = sendRest("MMS/Custom/edit",$_POST);
			$this->ajaxReturn($json);
			
		}else{
			
			$map['id'] = $this->_param("id","intval",0);
			$json = sendRest("MMS/Member/detail",$map);
			$this->assign("detail",$json['data']);
			
			$this->display();
		}
	}
	
	/**
	 * 查看会员资料
	 */
	public function view(){
		
		$map['id'] = $this->_param("id","intval",0);
		$json = sendRest("MMS/Member/detail",$map);
		$this->assign("detail",$json['data']);
			
		//获取该会员的订单信息
		$cat['dataid'] = $map['id'];
		$data = sendRest("OMS/Order/view",$cat);
		
		//会员地址列表
		$model   = M('Address');
		$address = $model->where('userid='.$map['id'].' and isdel=2')->select();
		$this->assign('address',$address);
		
		$this->assign("list",$data['data']);
		$this->assign("empty"," <tr><td colspan='6' align='center'>暂时没有订单信息</td></tr>");
		$this->assign("empty1","<tr><td colspan='6' align='center'>暂时没有地址信息</td></tr>");
		$this->display();
	}
	
	/**
	 * 冻结会员
	 */
	public function frost(){
	
		if(IS_AJAX){
			$_POST['status'] = 2;
			$json = sendRest("MMS/Custom/frost",$_POST);
			$this->ajaxReturn($json);
		}
	}
	/**
	 * 会员解冻
	 */
	public function thaw(){
	
		if(IS_AJAX){
			$_POST['status'] = 1;
			$json = sendRest("MMS/Custom/frost",$_POST);
			$this->ajaxReturn($json);
		}
	}
	
	/**
	 * 会员信息详情
	 */
	public function detail(){
		
		$map['customid'] = $this->_param("id","intval",0);
		$json = sendRest("MMS/Custom/view",$map);
			
		$map['company'] = 1;
		$data = sendRest("MMS/Vipcard/get",$map);
		
		$this->assign("vipArr",$data['data']);
		$this->assign("detail",$json['data']);
		$this->display();
	}
	
	/**
	 * 会员卡升级
	 */
	public function upgrade(){
		
		if(IS_AJAX){
			
			$map['id']       = $this->_get("vi_id", "intval",0);
			$map['cardid']   = $this->_get("vid",   "intval",0);
			$map['cardname'] = $this->_get("vtitle","trim",  "");
			
			$json = sendRest("MMS/Custom/upgrade",$map);
			
			$this->ajaxReturn($json);
			
		}else{
			
			$map['customid'] = $this->_get("id","intval",0);
			$json = sendRest("MMS/Custom/upgrade_use",$map);
			$this->assign("detail",$json['data']);
			$this->display();
		}
	}
	
	/**
	 * 删除（含批量删除）
	 */
	public  function del(){
		
		$map['id'] = $this->_param("id");
		
		$json = sendRest("MMS/Custom/del",$map);
		$this->ajaxReturn($json['data']);
	}
	
	/**
	 * 获取某个会员的积分操作记录
	 * @access public
	 * @param  Int customid 会员信息id
	 * @return array
	 */
	public function getCreditRecords(){
		
		$customid = $this->_param("customid","intval",0);
		$desc     = $this->_get("orderDirection","trim","desc");;
		$order    = $this->_get("orderName","trim","addtime");
		$pageNum  = $this->_get("pageNum","intval",C('PAGE_NUMS'));
		$nowPage  = $this->_get("p","intval",1);
			
		$map   = array("customid"=>$customid,"pageNum"=>$pageNum, "p"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);
		$json  = sendRest("MMS/Vipcard/getCreditRecords",$map);
		$data  = $json['data'];
		$count = $data['count'];
			
		$totalPage = ceil($count/$pageNum);
		$jsonData  = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
			
		foreach($data['list'] AS $row){
			$entry = array('id'=>$row['id'],
				'cell'=>array(
						'id'       => $row['id'],
						'company'  => $row['company'],
						'outletid' => $row['outletid'],
						'remark'   => $row['remark'],
						'val'      => $row['val'],
						'addid'    => $row['addid'],
						'addtime'  => date('Y-m-d',$row['addtime']),
						'act'      => $row['act']==1?"获得":"消费",
				),
			);
			$jsonData['rows'][] = $entry;
		}
			
		$this->ajaxReturn($jsonData,"JSON");
	}
	
	/**
	 * 获取某个会员的金额操作记录
	 * @access public
	 * @param  Int customid 会员信息id
	 * @return array
	 */
	public function getMoneyRecords(){
	
		$customid = $this->_param("customid","intval",0);
		$desc     = $this->_get("orderDirection","trim","desc");;
		$order    = $this->_get("orderName","trim","addtime");
		$pageNum  = $this->_get("pageNum","intval",C('PAGE_NUMS'));
		$nowPage  = $this->_get("p","intval",1);
			
		$map   = array("customid"=>$customid,"pageNum"=>$pageNum, "p"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);
		$json  = sendRest("MMS/Vipcard/getMoneyRecords",$map);
		$data  = $json['data'];
		$count = $data['count'];
			
		$totalPage = ceil($count/$pageNum);
		$jsonData  = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
			
		foreach($data['list'] AS $row){
			$entry = array('id'=>$row['id'],
					'cell'=>array(
							'id'       => $row['id'],
							'company'  => $row['company'],
							'remark'   => $row['remark'],
							'outletid' => $row['outletid'],
							'val'      => $row['val'],
							'addid'    => $row['addid'],
							'addtime'  => date('Y-m-d',$row['addtime']),
							'act'      => $row['act']==1?"获得":"消费",
					),
			);
			$jsonData['rows'][] = $entry;
		}
			
		$this->ajaxReturn($jsonData,"JSON");
	}
	
	
	/**
	 * 首页
	 */
	public function group(){
	
		if(IS_AJAX){
				
			$map['isdel'] = 2;
			$json = sendRest("MMS/Group/index",$map);
	
			foreach($json['data'] AS $row){
				$entry = array('id'=>$row['id'],
						'cell'=>array(
								'id'       => $row['id'],
								'title'    => $row['title'],
								'addtime'  => date('Y-m-d',$row['addtime']),
						),
				);
				$jsonData['rows'][] = $entry;
			}
	
			$this->ajaxReturn($jsonData,"JSON");
		}else{
			$this->display();
		}
	}
	
	/**
	 * 添加组员
	 */
	public function addMember(){
		
		if(IS_AJAX){
				
			$desc    = $this->_get("orderDirection","trim","desc");;
			$order   = $this->_get("orderName","trim","addtime");
			$pageNum = $this->_get("pageNum","intval",C('PAGE_NUMS'));
			$nowPage = $this->_get("p","intval",1);
			$where   = $this->_get("where","trim","");
				
			$map   = array("where"=>$where,"pageNum"=>$pageNum, "p"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);
			$json  = sendRest("MMS/Custom/customSel",$map);
			$data  = $json['data'];
			$count = $data['count'];
				
			$totalPage = ceil($count/$pageNum);
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
				
			foreach($data['list'] AS $row){
				$entry = array('id'=>$row['id'],
						'cell'=>array(
								'id'       => $row['id'],
								'realname' => $row['realname'],
								'mobile'   => $row['mobile'],
								'email'    => $row['email'],
								'sex'      => $row['sex']==1?"先生":"女士",),
				);
				$jsonData['rows'][] = $entry;
			}
				
			$this->ajaxReturn($jsonData,"JSON");
		}else{
			$id = $this->_get("id","intval",0);
			$map = array("id"=>$id);
			$json  = sendRest("MMS/Group/conSelect",$map);
			$this->assign("detail",$json['data']);
	
			$this->display();
		}
	}
	
	/**
	 * 新增分组
	 */
	public function groupAdd(){
	
		if(IS_AJAX){
			$json = sendRest("MMS/Group/add",$_POST);
			$this->ajaxReturn($json);
		}else{
			$this->display();
		}
	}
	
	/**
	 * 新增分组
	 */
	public function addMem(){
	
		if(IS_AJAX){
	
			$idStr   = $this->_get("id","trim","");
			$groupid = $this->_get("groupid","intval",0);
	
			$map  = array("idStr"=>$idStr,"groupid"=>$groupid);
			$json = sendRest("MMS/Group/addMem",$map);
	
			$this->ajaxReturn($json['data']);
		}
	}
	
	/**
	 * 查看组员
	 */
	public function groupMember(){
	
		if(IS_AJAX){
			
			$this->ajaxReturn($this->groupMemberSel(),"JSON");
				
		}else{
			$json  = sendRest("MMS/Group/index");
			$this->assign("detail",$json['data']);
				
			$this->display();
		}
	}
	
	
	
	/**
	 * 删除分组
	 */
	public function groupDel(){
	
		$map['id'] = $this->_get("id","intval",0);
		$json = sendRest("MMS/Group/del",$map);
		$this->ajaxReturn($json);
	}
	
	/**
	 * 删除会员会员分组
	 */
	public function memberGroupDel(){
	
		$map['customid'] = $this->_get("customid","trim","");
		$map['groupid']  = $this->_get("groupid","intval",0);
	
		$json = sendRest("MMS/Group/memberGroupDel",$map);
		$this->ajaxReturn($json);
	}
	
	/**
	 * 修改分组名称
	 */
	public function groupEdit(){
	
		if(IS_AJAX){
				
			$this->ajaxReturn($this->groupMemberSel(),"JSON");
				
		}else{
				
			$map['id'] = $this->_get("id","intval",0);
				
			$json      = sendRest("MMS/Group/edit",$map);
			$jsons     = sendRest("MMS/Group/index",$map);
				
			$this->assign("detail" , $json['data']);
			$this->assign("details", $jsons['data']);
				
			$this->display();
		}
	}
	
	/**
	 * 修改分组名称
	 */
	public function groupEdits(){
	
		if(IS_AJAX){
			$json = sendRest("MMS/Group/edit",$_POST,"post");
			$this->ajaxReturn($json);
		}
	}
	
	/**
	 * 修改会员分组
	 */
	public function groupMove(){
		
		if(IS_AJAX){
				
			$map['id']         = $this->_get("id",         "trim",   "");
			$map['groupid']    = $this->_get("groupid",    "intval", 0);
			$json = sendRest("MMS/Group/move",$map);
			$this->ajaxReturn($json);
		}
	}
	
	private function groupMemberSel(){
	
		$desc    = $this->_get("orderDirection","trim","desc");;
		$order   = $this->_get("orderName","trim","addtime");
		$pageNum = $this->_get("pageNum","intval",C('PAGE_NUMS'));
		$nowPage = $this->_get("p","intval",1);
		$where   = $this->_param('where','trim','');
		$groupid  = $this->_get("groupid","intval",0);
	
		$map   = array("groupid"=>$groupid,"where"=>$where,"pageNum"=>$pageNum, "p"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);
		$json  = sendRest("MMS/Group/groupMember",$map);
		$data  = $json['data'];
		$count = $data['count'];
	
		$totalPage = ceil($count/$pageNum);
		$jsonData  = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
	
		foreach($data['list'] AS $row){
			$entry = array('id'=>$row['id'],
					'cell'=>array(
							'id'       => $row['id'],
							'username' => $row['username'],
							'mobile'   => $row['mobile'],
							'email'    => $row['email'],
							'sex'      => $row['sex']==1?"先生":"女士",
					),
			);
			$jsonData['rows'][] = $entry;
		}
	
		return $jsonData;
	}
}
